package com.leo.Other;

import com.leo.offer.TreeNode;

/**
 * 判断一棵树是否是平衡二叉树
 * @author xuexiaolei
 * @version 2018年03月13日
 */
public class IsAVL {
    public static boolean isAVL(TreeNode node){
        if (node == null) { return true; }
        if (isAVL(node.left) && isAVL(node.right) && Math.abs(nodeLength(node.left)-nodeLength(node.right))<=1){
            return true;
        }else {
            return false;
        }
    }

    public static int nodeLength(TreeNode node){
        if (node!=null){
            return 1 + Math.max(nodeLength(node.left), nodeLength(node.right));
        }
        return 0;
    }
}
